package main

import (
	"fmt"
	"go-mvc-generator/core"
	"os"
	"path/filepath"
)

func main() {
	generater("sys_menu", "SysMenu", "sysMenu", "菜单")
	//clear()
}

func generater(tableName, StructName, structName string, moduleName string) {
	core.GetControllerGenerator.GenerateController(tableName, StructName, structName, moduleName)
	core.GetServiceGenerator.GenerateService(tableName, StructName, structName)
	core.GetDaoGenerator.GenerateDao(tableName, StructName, structName)
	core.GetModelGenerator.GenerateModel(tableName)
}

func clear() {
	dir, _ := os.Getwd()
	codePathController := dir + "/code/controller/"
	codePathService := dir + "/code/service/"
	codePathDao := dir + "/code/dao/"
	codePathModel := dir + "/code/model/"
	// 删除对应的controller、service、dao、model文件
	clearChild(codePathController)
	clearChild(codePathService)
	clearChild(codePathDao)
	clearChild(codePathModel)

	fmt.Println("文件删除成功")
}

func clearChild(dirPath string) {
	err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		// 如果是文件，删除文件
		if !info.IsDir() {
			err := os.Remove(path)
			if err != nil {
				fmt.Printf("Error deleting file %s: %v\n", path, err)
			} else {
				fmt.Printf("Deleted file: %s\n", path)
			}
		}
		return nil
	})
	if err != nil {
		fmt.Printf("Error walking through the directory: %v\n", err)
	} else {
		fmt.Println("All files deleted successfully.")
	}
}
